package net.tycmc.cty.ctyms.timejob.iems.thread;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import com.tycmc.functions.util.MapGetter;
import com.tycmc.functions.util.StringUtil;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.cty.ctyms.timejob.common.ConfigEnum;
import net.tycmc.cty.ctyms.timejob.common.DateControl;
import net.tycmc.cty.ctyms.timejob.function.mail.SendMail;

public class ThreadTerminateMaint implements Callable<String>{

	public Map<String,Object> param;
	/**
	 * @param args
	 */
	
	
	public ThreadTerminateMaint(Map<String,Object> map){
		this.param=map;
	}

	public String sendMes(Map<String,Object> mapLink,String content){
		String sql = "";
		if(null!=mapLink&&!mapLink.isEmpty()){
			String phone = mapLink.get("MtnLk_Phone")+"";
			String way = mapLink.get("MtnLk_RemindWay")+"";
			String MtnLkName = mapLink.get("MtnLk_Name")+"";
			
			String nowDate = DateControl.getDateTime(0);
			
			if(StringUtil.isValid(way)&&way.indexOf("1")!=-1){
				
				//存入短信发送表
				sql = sql +  "insert into SubTransQueue (OrderNumber,SourceNumber,MessageContent,TransportResult,TimingSendTime)"
						  + "values('"+phone+"','"+ConfigEnum.SourceNumber.getName()+"','"+content+"','-1',null);";
				//存入我短信记录表
				sql = sql +  "insert into Mtn_SubTransQueue_log (MtnSt_SqId,MtnSt_ReciveTime,MtnSt_RecivePerson,MtnSt_Phone,MtnSt_Content,MtnSt_Type,MtnSt_Way)"
						  + "values((select IDENT_CURRENT('MtnEnter_Info')),'"+nowDate+"','"+MtnLkName+"','"+phone+"','content','保养','自动');";
				
			}
		}
		return sql;
	}
	
	public void sendMial(Map<String,Object> mapLink,String content){

		if(null!=mapLink&&!mapLink.isEmpty()){
			//Email
			String Email = mapLink.get("MtnLk_Email")+""; 
			//人员名称
			String UserName = mapLink.get("MtnLk_Name")+"";
			//保养是提醒方式
			String way = mapLink.get("MtnLk_RemindWay")+"";

			if(StringUtil.isValid(way)&&way.indexOf("2")!=-1){
				//邮件开头
				String top = "尊敬的"+UserName+"客户，您好！ 保养提示信息如下，请注意。<br /><br/>";
				//汇总
				String collect = content+"<br/><br/>";
				
				List<String> list = new ArrayList<String>();
				list.add(Email);
				int result = SendMail.getInstance().sendMail("IEMS  保养信息", top+collect, list);
				if(result==2){
					System.out.println("---------------------------------------------------------------");
					System.out.println("给人员："+UserName+"发送到"+Email+"-------失败!");
					System.out.println("---------------------------------------------------------------");
				}
			}
		}
	}
	
	
	
	public String call() throws Exception {
		// TODO Auto-generated method stub

		String MtnasLastKM =MapGetter.getString(param, "mtnas_lastkm");  //上次保养里程
		String NowKm = MapGetter.getString(param, "nowkm");//当前进程
		String MtnTpSpaceKM = MapGetter.getString(param, "mtntp_spacekm");//保养间隔里程
		String MtnTpFloatKM = MapGetter.getString(param, "mtntp_floatkm");//保养浮动里程 
		String MtnAsType = MapGetter.getString(param, "mtnas_type");//保养状态
		MtnAsType=StringUtil.isValid(MtnAsType)?MtnAsType:"";
		//String MtnAsRemindTime = MapGetter.getString(param, "mtnas_remindtime");//保养提醒时间
		String MtnAsRemindKM= StringUtil.isValid(MapGetter.getString(param, "mtnas_remindkm"))?MapGetter.getString(param, "mtnas_remindkm"):null;//保养ID
		String VehicleId = MapGetter.getString(param, "vehicleid");//设备ID
		String VehicleNumber = MapGetter.getString(param, "vehiclenumber");//设备号
		String MtnAsTpId = MapGetter.getString(param, "mtnas_tpid");//保养类型
		String MtnTpName = MapGetter.getString(param, "mtntp_name");//保养类型名称
		String MtnAsPgId = MapGetter.getString(param, "mtnas_pgid");//保养ID
		String nextRemart = MapGetter.getString(param, "nextRemart");//上次保养备注
		String MtnAsRemark= StringUtil.isValid(MapGetter.getString(param, "mtnas_remark"))?"'"+MapGetter.getString(param, "mtnas_remark")+"'":null;//保养ID
		String MtnAsLastTime = MapGetter.getString(param, "mtnas_lastTime");//保养ID
		String MtnASNext =StringUtil.isValid( MapGetter.getString(param, "mtnas_next"))? MapGetter.getString(param, "mtnas_next"):null;//保养ID
		
		Double DbmtnasLastKM = Double.parseDouble(MtnasLastKM);
		Double DbnowKm = Double.parseDouble(NowKm);
		Double DbMtnTpSpaceKM =Double.parseDouble(MtnTpSpaceKM);
		Double DbMtnTpFloatKM =Double.parseDouble(MtnTpFloatKM);

		//保养短信信息
		String content = VehicleNumber+","+MtnTpName+", 剩余:"+new BigDecimal(DbmtnasLastKM+DbMtnTpSpaceKM-DbnowKm).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue()+",保养备注:"+nextRemart;
		
		String sql = "";
		//0 保养正常情况
		if(DbnowKm<DbmtnasLastKM+DbMtnTpSpaceKM-DbMtnTpFloatKM){
			if(!"0".equals(MtnAsType.trim())){//如果为0,则不更新
				Double NewMtnASNext = DbmtnasLastKM+DbMtnTpSpaceKM-DbMtnTpFloatKM;
				sql = sql + "update MtnProgram_AlertStat set MtnAS_Type=0 ,mtnas_remindtime=getdate(),mtnas_remindkm="+NowKm+",MtnAS_Next="+NewMtnASNext+" where MtnAS_VclId="+VehicleId+" and MtnAS_TpId="+MtnAsTpId+";";
				
				sql = sql + "insert into MtnProgram_AlertRecord(MtnAR_VclId,MtnAR_PgId,MtnAR_TpId,MtnAR_LastTime,MtnAR_LastKM,MtnAR_Type,MtnAR_RemindTime,MtnAR_RemindKM,MtnAR_Next,MtnAR_Remark)";
				sql = sql + " values("+VehicleId+","+MtnAsPgId+","+MtnAsTpId+",'"+MtnAsLastTime+"',"+MtnasLastKM+",0,getdate(),"+MtnAsRemindKM+","+NewMtnASNext+","+MtnAsRemark+");";
			}
		}
		
		//1 准备保养情况
		if((DbnowKm>=DbmtnasLastKM+DbMtnTpSpaceKM-DbMtnTpFloatKM)&&(DbnowKm<DbmtnasLastKM+DbMtnTpSpaceKM)){
			//如果为空更新保养更新时间
			if(!"1".equals(MtnAsType.trim())){
				sql = sql+sendMes((Map<String,Object>)param.get("linkman1"), content);
				sendMial((Map<String,Object>)param.get("linkman1"), content);
				
				Double NewMtnASNext = DbmtnasLastKM+DbMtnTpSpaceKM;
				sql = sql + "update MtnProgram_AlertStat set MtnAS_Type=1,mtnas_remindtime=getdate(),mtnas_remindkm="+NowKm+",MtnAS_Next="+NewMtnASNext+" where MtnAS_VclId="+VehicleId+" and MtnAS_TpId="+MtnAsTpId+";";	
				
				sql = sql + "insert into MtnProgram_AlertRecord(MtnAR_VclId,MtnAR_PgId,MtnAR_TpId,MtnAR_LastTime,MtnAR_LastKM,MtnAR_Type,MtnAR_RemindTime,MtnAR_RemindKM,MtnAR_Next,MtnAR_Remark)";
				sql = sql + "values("+VehicleId+","+MtnAsPgId+","+MtnAsTpId+",'"+MtnAsLastTime+"',"+MtnasLastKM+",1,getdate(),"+MtnAsRemindKM+","+NewMtnASNext+","+MtnAsRemark+");";
			}
		}
		
		//2 到期保养情况
		if((DbnowKm>=DbmtnasLastKM+DbMtnTpSpaceKM)&&(DbnowKm<DbmtnasLastKM+DbMtnTpSpaceKM+DbMtnTpFloatKM)){
			//如果为空更新保养更新时间
			if(!"2".equals(MtnAsType.trim())){
				sql = sql+sendMes((Map<String,Object>)param.get("linkman2"), content);
				sendMial((Map<String,Object>)param.get("linkman2"), content);
				
				Double NewMtnASNext = DbmtnasLastKM+DbMtnTpSpaceKM;
				sql = sql + "update MtnProgram_AlertStat set MtnAS_Type=2,mtnas_remindtime=getdate(),mtnas_remindkm="+NowKm+",MtnAS_Next="+NewMtnASNext+"  where MtnAS_VclId="+VehicleId+" and MtnAS_TpId="+MtnAsTpId+";";		
				
				sql = sql + "insert into MtnProgram_AlertRecord(MtnAR_VclId,MtnAR_PgId,MtnAR_TpId,MtnAR_LastTime,MtnAR_LastKM,MtnAR_Type,MtnAR_RemindTime,MtnAR_RemindKM,MtnAR_Next,MtnAR_Remark)";
				sql = sql + "values("+VehicleId+","+MtnAsPgId+","+MtnAsTpId+",'"+MtnAsLastTime+"',"+MtnasLastKM+",2,getdate(),"+MtnAsRemindKM+","+NewMtnASNext+","+MtnAsRemark+");";
			}
		}
		
		//3 超期保养情况
		if(DbnowKm>=DbmtnasLastKM+DbMtnTpSpaceKM+DbMtnTpFloatKM){
			//如果为空更新保养更新时间
			if(!"3".equals(MtnAsType.trim())){
				sql = sql+sendMes((Map<String,Object>)param.get("linkman3"), content);
				sendMial((Map<String,Object>)param.get("linkman3"), content);
				
				sql = sql + "update MtnProgram_AlertStat set MtnAS_Type=3,mtnas_remindtime=getdate(),mtnas_remindkm="+NowKm+"  where MtnAS_VclId="+VehicleId+" and MtnAS_TpId="+MtnAsTpId+";";	
				
				sql = sql + "insert into MtnProgram_AlertRecord(MtnAR_VclId,MtnAR_PgId,MtnAR_TpId,MtnAR_LastTime,MtnAR_LastKM,MtnAR_Type,MtnAR_RemindTime,MtnAR_RemindKM,MtnAR_Next,MtnAR_Remark)";
				sql = sql + "values("+VehicleId+","+MtnAsPgId+","+MtnAsTpId+",'"+MtnAsLastTime+"',"+MtnasLastKM+",3,getdate(),"+MtnAsRemindKM+","+MtnASNext+","+MtnAsRemark+");";
			}

		}
		
		
		
		return sql;
	}

}
